package com.serkol.handler;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

//@EnableTransactionManagement
@Configuration
@MapperScan("com.serkol.ams.mapper")
public class MybatisPlusConfig implements InitializingBean {
    @Autowired
    private DataSource dataSource;
    @Value("${backup.path}")
    private String backupPath;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;


    private BackupService backupService;


    //  @Bean
/*    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        return interceptor;
    }*/

    @Bean
    public MyMetaObjectHandler myLogicSqlInjector() {
        return new MyMetaObjectHandler();
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 取消MyBatis Plus的最大分页500条的限制
        paginationInnerInterceptor.setMaxLimit(100000L);
        paginationInnerInterceptor.setOverflow(true);
        DynamicTableNameInnerInterceptor  myDynamicTableNameInterceptor = new DynamicTableNameInnerInterceptor();

        //map.put("users", new DynamicTableNameHandler());
        myDynamicTableNameInterceptor.setTableNameHandler(new DynamicTableNameHandler());
        interceptor.addInnerInterceptor(myDynamicTableNameInterceptor);
        //  interceptor.addInnerInterceptor(innerInterceptor);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;


    }

    private String getTableName(String sql, String tableName) {
        System.out.println("Index1  sssssssssssssssssssssssssssssssss:"+tableName);

        return tableName;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        backupService = new BackupService(dataSource, backupPath,username,password);
        backupService.backup();
    }
}
